import model


class UmlValidate:
    def validate(self, mod: model.UmlModel):
        self.model = mod
        self.definedTypes: dict[str, model.UmlElement] = {}
        self.__get_primitive_types()
        self.__get_types(self.model.package)
        self.__get_types(self.model.profile)
        self.__valid_package(self.model.package)
        self.__valid_package(self.model.profile)

    def __get_primitive_types(self):
        self.definedTypes[model.UmlXmiInfo.type_boolean] = "bool"
        self.definedTypes[model.UmlXmiInfo.type_integer] = "int"
        self.definedTypes[model.UmlXmiInfo.type_real] = "double"
        self.definedTypes[model.UmlXmiInfo.type_string] = "QString"
        self.definedTypes[model.UmlXmiInfo.type_unlimited] = "int"

    def __get_types(self, package: model.UmlPackage | model.UmlProfile) -> None:
        if package is None:
            return

        for element in package.packagedElements.values():
            if element.xmi_type == model.UmlXmiInfo.type_package:
                self.__get_types(element)
            elif element.xmi_type == model.UmlXmiInfo.type_class:
                self.definedTypes[element.name] = element
            elif element.xmi_type == model.UmlXmiInfo.type_data_type:
                self.definedTypes[element.name] = element
            elif element.xmi_type == model.UmlXmiInfo.type_enumeration:
                self.definedTypes[element.name] = element
            elif element.xmi_type == model.UmlXmiInfo.type_primitive_type:
                self.definedTypes[element.name] = element
            elif element.xmi_type == model.UmlXmiInfo.type_extension:
                # self.__valid_extension(element)
                pass
            elif element.xmi_type == model.UmlXmiInfo.type_association:
                pass
            elif element.xmi_type == model.UmlXmiInfo.type_package_import:
                pass
            else:
                print(element.xmi_type, element.xmi_id)

    def __valid_package(self, package: model.UmlPackage | model.UmlProfile) -> None:
        if package is None:
            return

        for element in package.packagedElements.values():
            if element.xmi_type == model.UmlXmiInfo.type_package:
                self.__valid_package(element)
            elif element.xmi_type == model.UmlXmiInfo.type_class:
                self.__valid_class(element)
            elif element.xmi_type == model.UmlXmiInfo.type_data_type:
                self.__valid_class(element)
            elif element.xmi_type == model.UmlXmiInfo.type_enumeration:
                self.__valid_enum(element)
            elif element.xmi_type == model.UmlXmiInfo.type_primitive_type:
                pass
            elif element.xmi_type == model.UmlXmiInfo.type_extension:
                # self.__valid_extension(element)
                pass
            elif element.xmi_type == model.UmlXmiInfo.type_association:
                pass
            elif element.xmi_type == model.UmlXmiInfo.type_package_import:
                pass
            else:
                print(element.xmi_type, element.xmi_id)

    def __valid_class(self, element: model.UmlClass) -> None:
        for attr in element.ownedAttributes:
            if attr.type is None:
                print("None", attr.xmi_id)
                continue
            if self.definedTypes.get(attr.type.href) is None:
                print(attr.type.href, attr.xmi_id)

    def __valid_enum(self, element: model.UmlEnumeration) -> None:
        pass
